﻿using System;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Regions;
using Xceed.Wpf.Toolkit;
using YunBox2.Client.Infrastructure;
using YunBox2.Contracts;
using YunBox2.Contracts.Core;
using YunBox2.Contracts.FoundationModels;

using Microsoft.Practices.Prism.Events;
namespace YunBox2.Client.Foundation.ViewModels
{
	[Export]
	public class BoardMaterialEditorViewModel : DomainObject, IConfirmNavigationRequest
	{
		private readonly IFoundationService _foundationService = ClientContext.Current.FoundationService;
		private BoardMaterial _dataSource;




		[Import]
		private IRegionManager _regionManager;

		[Import]
		private IEventAggregator _eventAggregator;

		[ImportingConstructor]
		public BoardMaterialEditorViewModel()
		{
		}

		public BoardMaterial DataSource
		{
			get
			{
				return _dataSource;
			}
			private set
			{
				if (_dataSource != value)
				{
					_dataSource = value;
					RaisePropertyChanged("DataSource");
				}
			}
		}
		#region 状态
		private bool _isCreating;
		public bool IsCreating
		{
			get
			{
				return _isCreating;
			}
			set
			{
				if(_isCreating==value)
				{
					_isCreating = value;
					RaisePropertyChanged("IsCreating", "CreatingMessage", "IsReadonly", "HasChanged");
				}
			}
		}

		public string CreatingMessage
		{
			get
			{
				return IsCreating ? "新建" : string.Empty;
			}
		}

		private bool _isReadonly=true;
		public bool IsReadonly
		{
			get
			{
				return !IsCreating || _isReadonly || _dataSource==null;
			}
			private set
			{
				if(_isReadonly==value)
				{
					_isReadonly = value;
					RaisePropertyChanged("IsReadonly", "HasChanged");
				}
			}
		}

		private bool _hasChanged;
		public bool HasChanged
		{
			get
			{
				return !IsReadonly && _hasChanged;
			}
			set
			{
				if(_hasChanged==value)
				{
					_hasChanged = value;
					RaisePropertyChanged("HasChanged");
				}
			}
		}
		#endregion

		#region IConfirmNavigationRequest 成员

		public bool IsNavigationTarget(NavigationContext navigationContext)
		{
			return true;
		}

		public void OnNavigatedFrom(NavigationContext navigationContext)
		{
			
		}

		public void OnNavigatedTo(NavigationContext navigationContext)
		{
			string action = navigationContext.Parameters["action"];
			if (action == "new")
			{
				DataSource = new BoardMaterial();
				
			}
			else
			{
				string strId = navigationContext.Parameters["id"];
				Guid id;
				if (Guid.TryParse(strId,out id))
				{
					DataSource = _foundationService.GetBoardMaterialById(ClientContext.Current.ClientID, id);
				}
				else
				{
					MessageBox.Show("参数传递错误!");
				}
			}
		}

		public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
		{

		}

		#endregion
	}
}